//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.k3cloud.user.listener;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.hustcad.plm.pdm.flowbusiness.model.entity.FlowObject;
import com.hustcad.plm.pdm.flowbusiness.model.entity.WorkFlowObjectPackage;
import com.hustcad.plm.pdm.workflow.annotion.CustomListener;
import com.hustcad.plm.pdm.workflow.annotion.CustomListenerField;
import com.hustcad.plm.pdm.workflow.model.entity.WorkFlowContainerView;
import com.hustcad.plm.pdm.workflow.service.TyplmWorkFlowContainerService;
import com.k3cloud.base.type.ClassificationTypeEnum;
import com.k3cloud.base.type.OutJsonTypeEnum;
import com.k3cloud.rpc.core.SendHttpServer;
import com.ty.basic.exception.message.SystemErrorCodeEnum;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.impl.el.FixedValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigInteger;


@CustomListener(
        category = "永续用户金蝶监听器",
        description = "永续用户金蝶监听器"
)
public class K3CloudListener implements ExecutionListener {
    private static final Logger log = LoggerFactory.getLogger(K3CloudListener.class);
    private static final long serialVersionUID = -9059663559349037414L;

    @CustomListenerField(
            description = "ERP接口编码",
            defaultValue = "",
            required = true
    )
    private FixedValue code;

    @CustomListenerField(
            description = "ERP接口对象",
            defaultValue = "",
            required = true
    )
    private FixedValue erpObject;


    public K3CloudListener() {

    }

    @Override
    public void notify(DelegateExecution execution) {
        try {
            log.info("开始执行监听 ==> 物料申请MaterialApplicationListener");
            String workFlowContainerIdStr = execution.getProcessInstanceBusinessKey();
            BigInteger workFlowContainerId = new BigInteger(workFlowContainerIdStr);
            TyplmWorkFlowContainerService typlmWorkFlowContainerService = SpringUtil.getBean(
                    TyplmWorkFlowContainerService.class);
            if (typlmWorkFlowContainerService != null) {
                WorkFlowContainerView workFlowContainerView = typlmWorkFlowContainerService.getWorkFlowContainerDetailByPrimaryKey(
                        workFlowContainerId);
                if (workFlowContainerView instanceof WorkFlowObjectPackage) {
                    WorkFlowObjectPackage workFlowObjectPackage = (WorkFlowObjectPackage) workFlowContainerView;
                    if (workFlowObjectPackage.getFlowObjectList() != null && workFlowObjectPackage.getFlowObjectList()
                            .size() != 0) {
                        for (int i = 0; i < workFlowObjectPackage.getFlowObjectList().size(); i++) {
                            FlowObject flowObject = workFlowObjectPackage.getFlowObjectList().get(i);
                            String oType = flowObject.getRefObjectOtype();
                            if ("ty.inteplm.part.CTyPart".equals(oType)) {
                                SendHttpServer sendHttpServer = SpringUtil.getBean(SendHttpServer.class);

                                JSONObject resultHttp=sendHttpServer.sendUserDataToServer(this.code.getExpressionText(),
                                                                                          this.erpObject.getExpressionText(),
                                                                                          OutJsonTypeEnum.OUT_JSON_TYPE_K3CLOUD,
                                                                                          ClassificationTypeEnum.DEFAULT.setValue("MaterialsHandling"),
                                                                                          flowObject, workFlowContainerView);
                                if(resultHttp.containsKey("Result")==false){
                                    throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(resultHttp);
                                }
                                JSONObject Result=resultHttp.getJSONObject("Result");
                                if(Result.containsKey("ResponseStatus")==false){
                                    throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(Result);
                                }
                                JSONObject ResponseStatus=Result.getJSONObject("ResponseStatus");
                                if(ResponseStatus.getBoolean("IsSuccess")==false){
                                    throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(ResponseStatus.getJSONArray("Errors"));
                                }
                            }
                        }
                    }
                }
            } else {
                throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException("TyplmWorkFlowContainerService无法加载");
            }

            log.info("正常执行监听结束 <== 物料申请MaterialApplicationListener");
        } catch (Exception var5) {
            log.error("监听MaterialApplicationListener异常：", var5);
            throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(var5.getMessage());
        }
    }

}
